package com.weetop.aklyscan.activity;

import android.content.Intent;
import android.content.res.AssetFileDescriptor;
import android.content.res.AssetManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.widget.NestedScrollView;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.classic.adapter.BaseAdapterHelper;
import com.classic.adapter.CommonRecyclerAdapter;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.interfaces.OnSelectListener;
import com.qmuiteam.qmui.layout.QMUIButton;
import com.weetop.aklyscan.R;
import com.weetop.aklyscan.base.BaseFragment;
import com.weetop.aklyscan.base.BaseUrl;
import com.weetop.aklyscan.base.mvp.BaseModel;
import com.weetop.aklyscan.bean.FittingDetailBean;
import com.weetop.aklyscan.bean.InStockItemBean;
import com.weetop.aklyscan.bean.IntoProductSaveBean;
import com.weetop.aklyscan.bean.OnlineOutStockItemBean;
import com.weetop.aklyscan.bean.OutStockItemBean;
import com.weetop.aklyscan.bean.ScanChukuBean;
import com.weetop.aklyscan.bean.ZXGoodsInfo;
import com.weetop.aklyscan.presenter.ScanOutPresent;
import com.weetop.aklyscan.utils.BaseUtils;
import com.weetop.aklyscan.utils.widget.MyDialogUtil;
import com.weetop.aklyscan.view.ScanOutView;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import cc.shinichi.library.tool.utility.ui.ToastUtil;

public class Fragment_out_wx extends BaseFragment<ScanOutPresent> implements ScanOutView, CommonRecyclerAdapter.OnItemClickListener {
    private static final int HUOJIA_CODE = 12000;
    private static final int REQUEST_HUOJIA_CODE_SCAN_ONE = 22000;

    @BindView(R.id.rcy_data)
    RecyclerView rcyData;
    @BindView(R.id.rcy_data2)
    RecyclerView rcyData2;
    @BindView(R.id.rcy_data3)
    RecyclerView rcyData3;
    Unbinder unbinder;
    @BindView(R.id.order_type_spinner)
    TextView orderTypeSpinner;
    @BindView(R.id.peijian_type_spinner)
    TextView peijianTypeSpinner;
    @BindView(R.id.tv_order_code)
    TextView tvOrderCode;
    @BindView(R.id.tv_chuku_zongliang)
    EditText tvChukuZongliang;
    @BindView(R.id.btn_chuku_save)
    QMUIButton btnChukuSave;
    @BindView(R.id.scan_out_rootview)
    NestedScrollView scanOutRootview;
    @BindView(R.id.tv_totle_postion)
    TextView tvTotlePostion;

    @BindView(R.id.chanpin_ll)
    LinearLayout chanpin_ll;
    @BindView(R.id.peijian_ll)
    LinearLayout peijian_ll;
    @BindView(R.id.peijian_ll1)
    LinearLayout peijian_ll1;
    @BindView(R.id.xinghao)
    TextView xinghao;
    @BindView(R.id.mingcheng)
    TextView mingcheng;
    @BindView(R.id.qingkong)
    TextView qingkong;


    @BindView(R.id.ll_box_code)
    View boxCodeView;
    @BindView(R.id.tv_stock_code)
    TextView stockCodeView;
    @BindView(R.id.tv_change_code)
    TextView boxCodeTextView;
    @BindView(R.id.et_peijian_code)
    EditText peijianCodeEditText;
    @BindView(R.id.tv_matter_name)
    TextView matterNameView;
    @BindView(R.id.tv_matter_num)
    EditText matterNumView;
    @BindView(R.id.tv_transit_code_menu)
    TextView tv_transit_code_menu;
    @BindView(R.id.tv_transit_code_menu_line)
    View tv_transit_code_menu_line;
    private CommonRecyclerAdapter<OutStockItemBean.ShopData> adapter1;
    private CommonRecyclerAdapter<FittingDetailBean> adapter12;
    private CommonRecyclerAdapter<InStockItemBean> adapter2;
    private CommonRecyclerAdapter<InStockItemBean> adapter3;
    // 产品类型：1产品；2配件
    private int inStockType = 1;
    // 扫码类型：1编号；2货架；3产品
    private int scanType = 1;
    private String orderCode;
    private List<OutStockItemBean.ShopData> productISList;
    private List<FittingDetailBean> fittingDetailBeanList;
    private List<InStockItemBean> list = new ArrayList<>();
    private String barcode;
    private HashMap<String, Integer> fittingMap;

    int goodNum = 0;
    int isDan = 1, isChanpin = 1;
    int isOnline = 1;
    boolean isBox = false;
    String xiangCode, huoCode;

    @Override
    protected ScanOutPresent createPresenter() {
        return new ScanOutPresent(this, mActivity);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = View.inflate(getActivity(), R.layout.activity_scan_out, null);
        unbinder = ButterKnife.bind(this, view);
        initView();
        return view;
    }

    private void initView() {
        rcyData2.setLayoutManager(new LinearLayoutManager(mActivity));
        adapter2 = new CommonRecyclerAdapter<InStockItemBean>(mActivity, R.layout.item_scanout2, list) {
            @Override
            public void onUpdate(BaseAdapterHelper helper, InStockItemBean item, int position) {
                helper.setText(R.id.tv_xuhao, position + 1 + "");
                helper.setText(R.id.tv_huopin_code, item.getGoods_code());
                helper.setText(R.id.tv_huopin_xinghao, item.getGoods_model());
                helper.setText(R.id.tv_huopin_mingcheng, item.getGoods_name());
                String type = item.getGoods_type() == 1 ? "产品" : "配件";
                helper.setText(R.id.tv_suoshu_huojia_zuobiao, item.getWarehouse_coordinates());
                helper.setText(R.id.tv_zzx, item.getBarcode() == null ? "" : item.getBarcode());
//                helper.setVisible(R.id.btn_delete, View.INVISIBLE);
                helper.setOnClickListener(R.id.btn_delete, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        adapter2.remove(item);
                        list.remove(item);
                        adapter2.notifyDataSetChanged();
                    }
                });
            }
        };
        rcyData2.setAdapter(adapter2);

        rcyData3.setLayoutManager(new LinearLayoutManager(mActivity));
        adapter3 = new CommonRecyclerAdapter<InStockItemBean>(mActivity, R.layout.item_in_wx_peijian3, list) {
            @Override
            public void onUpdate(BaseAdapterHelper helper, InStockItemBean item, int position) {
                helper.setVisible(R.id.tv_zzx, isBox);
                helper.setVisible(R.id.tv_zzx_line, isBox);
                helper.setText(R.id.tv_xuhao, position + 1 + "");
                helper.setText(R.id.tv_huopin_sn, item.getGoods_code());
                helper.setText(R.id.tv_huopin_xinghao, item.getGoods_code() + "");
                helper.setText(R.id.tv_huopin_mingcheng, item.getGoods_name());
                helper.setText(R.id.tv_xiangnei_huojia, item.getWarehouse_coordinates());
                helper.setText(R.id.tv_zzx, TextUtils.isEmpty(item.getTransit_code()) ? "" : item.getTransit_code());
//                helper.setText(R.id.tv_transit_code_menu, )
                helper.setText(R.id.tv_peijian_num, item.getCount() + "");
//                helper.setVisible(R.id.btn_delete, View.INVISIBLE);
                helper.setOnClickListener(R.id.btn_delete, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        adapter3.remove(item);
                        list.remove(item);
                        adapter3.notifyDataSetChanged();
                    }
                });
            }
        };
        rcyData3.setAdapter(adapter3);
        tv_transit_code_menu.setVisibility(View.GONE);
        tv_transit_code_menu_line.setVisibility(View.GONE);
//        onBarcodeEvent("SHWX202202210001");
//        orderCode = "akny05-PJCG202108300005";
//        tvOrderCode.setText("SHWX202201220001");
//        handler.postDelayed(new Runnable() {
//            @Override
//            public void run() {
//                onBarcodeEvent("M1001akny022128C3598");
////                handler.postDelayed(new Runnable() {
////                    @Override
////                    public void run() {
////                        onBarcodeEvent("M1001akny022128C3605");
////                    }
////                }, 4000);
//            }
//        }, 1000);

    }

    Handler handler = new Handler();

    @Override
    public void onItemClick(RecyclerView.ViewHolder viewHolder, View view, int position) {
//        if (inStockType == 1) {
//            mPresenter.scanProduct(productISList.get(position).getCode());
//        } else {
//            mPresenter.scanFitting(productISList.get(position).getCode());
//        }
    }

    @Override
    public void getOut01Success(BaseModel<OutStockItemBean> o) {
//        mPresenter.Out04("F1001akny022128C3688");
//        mPresenter.Out04("F1002akny022128C3664");
        productISList = o.getData().getShop_data();
        tvOrderCode.setText(orderCode);
        goodNum = o.getData().getAll_count();
        tvTotlePostion.setText(o.getData().getAll_count() + "");
        if (productISList != null && productISList.size() > 0) {
            rcyData.setLayoutManager(new LinearLayoutManager(mActivity));
            adapter1 = new CommonRecyclerAdapter<OutStockItemBean.ShopData>(mActivity, R.layout.item_scanout_out1, productISList) {
                @Override
                public void onUpdate(BaseAdapterHelper helper, OutStockItemBean.ShopData item, int position) {
                    helper.setText(R.id.tv_xuhao, position + 1 + "");
                    helper.setText(R.id.tv_huopin_xinghao, item.getGoods_model());
                    helper.setText(R.id.tv_huopin_mingcheng, item.getGoods_name());
                    helper.setText(R.id.tv_yichu, item.getYc_num());
                    helper.setText(R.id.tv_weichu, item.getWc_num());
                    helper.setText(R.id.tv_huopin_dindan, item.getNum() + "");
                }
            };
            adapter1.setOnItemClickListener(this);
            rcyData.setAdapter(adapter1);
            playRing("shujuyouxiao.mp3");
        } else {
            adapter1.clear();
            adapter1.notifyDataSetChanged();
            playRing("shujuwuxiao.mp3");
        }

    }

    @Override
    public void getDetailSuccess(BaseModel<OnlineOutStockItemBean> o) {
        productISList = o.getData().getItems();
        tvOrderCode.setText(orderCode);
        goodNum = Integer.parseInt(o.getData().getInfo().getAll_count());
        tvTotlePostion.setText(o.getData().getInfo().getAll_count());
        if (productISList != null && productISList.size() > 0) {
            rcyData.setLayoutManager(new LinearLayoutManager(mActivity));
            adapter1 = new CommonRecyclerAdapter<OutStockItemBean.ShopData>(mActivity, R.layout.item_scanout_out1, productISList) {
                @Override
                public void onUpdate(BaseAdapterHelper helper, OutStockItemBean.ShopData item, int position) {
                    helper.setText(R.id.tv_xuhao, position + 1 + "");
                    helper.setText(R.id.tv_huopin_xinghao, item.getGoods_model());
                    helper.setText(R.id.tv_huopin_mingcheng, item.getGoods_name());
                    helper.setText(R.id.tv_yichu, item.getYck_count());
                    helper.setText(R.id.tv_weichu, item.getWck_count());
                    helper.setText(R.id.tv_huopin_dindan, item.getNum() + "");
                }
            };
            adapter1.setOnItemClickListener(this);
            rcyData.setAdapter(adapter1);
            playRing("shujuyouxiao.mp3");
        } else {
            adapter1.clear();
            adapter1.notifyDataSetChanged();
            playRing("shujuwuxiao.mp3");
        }
    }

    private Boolean isSh = false;

    @Override
    public void getPeiJianDetailSuccess(BaseModel<OnlineOutStockItemBean> o) {
        productISList = o.getData().getItems();
        tvOrderCode.setText(orderCode);
//        goodNum = Integer.parseInt(o.getData().getInfo().getAll_count());
        tvTotlePostion.setText(o.getData().getInfo().getAll_count());
        if (StringUtils.equalsIgnoreCase(o.getData().getInfo().getOrder_shwx_type(), "1")) {
            isSh = true;
        }
        if (productISList != null && productISList.size() > 0) {
            rcyData.setLayoutManager(new LinearLayoutManager(mActivity));
            adapter1 = new CommonRecyclerAdapter<OutStockItemBean.ShopData>(mActivity, R.layout.item_scanout_out1, productISList) {
                @Override
                public void onUpdate(BaseAdapterHelper helper, OutStockItemBean.ShopData item, int position) {
                    helper.setText(R.id.tv_xuhao, position + 1 + "");
                    helper.setText(R.id.tv_huopin_xinghao, item.getMatter_code());
                    helper.setText(R.id.tv_huopin_mingcheng, item.getMatter_name());
                    helper.setText(R.id.tv_yichu, item.getYck_count());
                    helper.setText(R.id.tv_weichu, item.getWck_count());
                    helper.setText(R.id.tv_huopin_dindan, item.getNum() + "");
                }
            };
            adapter1.setOnItemClickListener(this);
            rcyData.setAdapter(adapter1);
            playRing("shujuyouxiao.mp3");
        } else {
            adapter1.clear();
            adapter1.notifyDataSetChanged();
            playRing("shujuwuxiao.mp3");
        }
    }

    @Override
    public void getInto08Success(BaseModel<ZXGoodsInfo> o) {
        ZXGoodsInfo data = o.getData();
        InStockItemBean goodsItems = new InStockItemBean();
        goodsItems.setGoods_code(data.getSerial_code());
        goodsItems.setGoods_name(data.getGoods_name());
        goodsItems.setGoods_model(data.getGoods_model());
        goodsItems.setBarcode(data.getBarcode());
        goodsItems.setAudio_url(data.getAudio_url());
        goodsItems.setCount(1);
        //状态:-1已作废；0未入库；1在库里；2未出库；3已出库
        goodsItems.setStatus(data.getStatus());
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getGoods_code().equals(goodsItems.getGoods_code())) {
                MyDialogUtil.showDialog(getActivity(), "数据无效已删除");
//                playUrl(goodsItems.getAudio_url(), 0);
                playUrl("", 0);
//                playRing("wuxiao.mp3");
                return;
            }
        }
        for (int i = 0; i < productISList.size(); i++) {
            if (goodsItems.getGoods_model().equals(productISList.get(i).getGoods_model())) {
                list.add(goodsItems);
                adapter2.add(goodsItems);
                rcyData2.smoothScrollToPosition(list.size());
                playUrl(goodsItems.getAudio_url(), 1);
//                playRing("luruchenggong.mp3");
                return;
            }
        }
        MyDialogUtil.showDialog(getActivity(), "数据无效已删除");
//        playUrl(goodsItems.getAudio_url(), 0);
        playUrl("", 0);
//        playRing("wuxiao.mp3");
    }

    @Override
    public void getOut03Success(BaseModel<String> o) {
        ToastUtil.getInstance()._short(getActivity(), "操作成功");
        list.clear();
        adapter1.clear();
        adapter1.notifyDataSetChanged();
        adapter2.clear();
        adapter2.notifyDataSetChanged();
        adapter3.clear();
        adapter3.notifyDataSetChanged();
        tvTotlePostion.setText("");
        playRing("chukuwancheng.mp3");
    }

    @Override
    public void getInto13Success(BaseModel<Object> o) {
        stockCodeView.setText(huoCode);
        stockCodeView.setBackgroundResource(R.drawable.et_grey);
//        if (TextUtils.isEmpty(zhongzhuanxiang.getText().toString().trim())) {
//            zhongzhuanxiang.setBackgroundResource(R.drawable.et_yellow);
//        }
//        if (TextUtils.isEmpty(peijian_no.getText().toString().trim())) {
//            peijian_no.setBackgroundResource(R.drawable.et_yellow);
//        }
        playRing("shujuyouxiao.mp3");
    }

    @Override
    public void saveZxSuccess(BaseModel<Object> o) {
        boxCodeTextView.setText(xiangCode);
        boxCodeTextView.setBackgroundResource(R.drawable.et_grey);

//        if (TextUtils.isEmpty(peijian_no.getText().toString().trim())) {
//            peijian_no.setBackgroundResource(R.drawable.et_yellow);
//        } else {
//            peijian_no.setBackgroundResource(R.drawable.et_grey);
//        }
        playRing("shujuyouxiao.mp3");
//        mPresenter.Into46(tvOrderCode.getText().toString().trim());
    }

    @Override
    public void getOut04Success(BaseModel<ScanChukuBean> o) {
        ScanChukuBean data = o.getData();
        InStockItemBean goodsItems = new InStockItemBean();
        adapter2.notifyDataSetChanged();
        adapter3.notifyDataSetChanged();
        if (isChanpin != 1) {
            //配件
            goodsItems.setGoods_code(data.getMatter_code());
            goodsItems.setGoods_name(data.getMatter_name());
            goodsItems.setGoods_model(data.getTransit_code());
            goodsItems.setWarehouse_coordinates(data.getWarehouse_coordinates());
            goodsItems.setCount(Integer.parseInt(data.getNum()));
            goodsItems.setTransit_code(data.getTransit_code());
            matterNameView.setText(o.getData().getMatter_name());
        } else {
            goodsItems.setGoods_code(data.getSerial_code());
            goodsItems.setGoods_name(data.getGoods_name());
            goodsItems.setGoods_model(data.getGoods_model());
            goodsItems.setWarehouse_coordinates(data.getWarehouse_coordinates());
            goodsItems.setCount(1);
        }

//        int defaultIndex = -1;
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getGoods_code().equals(goodsItems.getGoods_code())) {
//                defaultIndex = i;
                MyDialogUtil.showDialog(getActivity(), "数据无效已删除");
//                playUrl(goodsItems.getAudio_url(), 0);
                playUrl("", 0);
                return;
            }
        }
//        list.remove(defaultIndex);
        for (int i = 0; i < productISList.size(); i++) {
            if (isChanpin == 1) {
                if (goodsItems.getGoods_model().equals(productISList.get(i).getGoods_model())) {
                    list.add(goodsItems);
                    adapter2.add(goodsItems);

                    rcyData2.smoothScrollToPosition(list.size());
                    playUrl(goodsItems.getAudio_url(), 1);
//                playRing("luruchenggong.mp3");
                    return;
                }
            } else {
                if (goodsItems.getGoods_code().equals(productISList.get(i).getMatter_code())) {
                    list.add(goodsItems);
                    adapter3.add(goodsItems);

                    rcyData3.smoothScrollToPosition(list.size());
                    playUrl(goodsItems.getAudio_url(), 1);
//                playRing("luruchenggong.mp3");
                    return;
                }
            }

        }
        MyDialogUtil.showDialog(getActivity(), "数据无效已删除");
//        playUrl(goodsItems.getAudio_url(), 0);
        playUrl("", 0);
    }

    @Override
    public void saveOutKuSuccess(BaseModel<Object> o) {
        ToastUtils.showShort("出库成功");
    }

    @Override
    public void showError(String msg) {
        super.showError(msg);
        ToastUtil.getInstance()._short(getActivity(), msg);
    }

    @Override
    public void onDataError() {

    }

    @Override
    public void onUpFail(String mag) {
        MyDialogUtil.showDialog(getActivity(), mag);
        playRing("wuxiao.mp3");
    }

    @OnClick({R.id.stock_code_clear, R.id.box_code_clear, R.id.tv_matter_clear, R.id.confirm,
            R.id.record,
            R.id.peijian_type_spinner, R.id.order_type_spinner, R.id.btn_chuku_save, R.id.tv_order_code, R.id.button, R.id.qingkong})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.record:
                if (TextUtils.isEmpty(tvOrderCode.getText().toString().trim())) {
                    ToastUtils.showShort("没有出库凭证编号");
                    return;
                }
                Intent intent = new Intent(getActivity(), OutRecordListActivity.class);
                int order_type = OutRecordListActivity.ORDER_TYPE_ONLINE_PP;
                if (isChanpin == 1) {
                    //线上线下
                    if (isOnline == 1) {
                        //线上
                        order_type = OutRecordListActivity.ORDER_TYPE_ONLINE_PP;
                    } else {
                        //线下
                        order_type = OutRecordListActivity.ORDER_TYPE_OFFLINE_PP;
                    }
                } else {
                    //配件
                    order_type = OutRecordListActivity.ORDER_TYPE_MATTER_PP;
                }
                intent.putExtra("order_type", order_type);
                intent.putExtra("order_sn", tvOrderCode.getText().toString().trim());
                getActivity().startActivity(intent);
                break;
            case R.id.confirm:
                //验证配件
                String stockCode = stockCodeView.getText().toString().trim();
                String boxCode = boxCodeTextView.getText().toString().trim();
                String matterCode = peijianCodeEditText.getText().toString().trim();
                String num = matterNumView.getText().toString().trim();
                if (TextUtils.isEmpty(stockCode)) {
                    ToastUtils.showShort("货架号为空");
                    return;
                }
                if (TextUtils.isEmpty(matterCode)) {
                    ToastUtils.showShort("配件编号为空");
                    return;
                }
                if (TextUtils.isEmpty(num)) {
                    ToastUtils.showShort("数量为空");
                    return;
                }
                if (isBox && TextUtils.isEmpty(stockCode)) {
                    ToastUtils.showShort("中转箱号为空");
                    return;
                }
                if (!isBox) {
                    boxCode = "";
                }
                mPresenter.Out06(orderCode, stockCode, boxCode, matterCode, num);
                break;
            case R.id.tv_matter_clear:
                playRing("qingkong.mp3");
                peijianCodeEditText.setText("");
                matterNameView.setText("");
                matterNumView.setText("");
                break;
            case R.id.box_code_clear:
                playRing("qingkong.mp3");
                boxCodeTextView.setText("");
                break;
            case R.id.stock_code_clear:
                //货架号清空
                playRing("qingkong.mp3");
                stockCodeView.setText("");
                peijianCodeEditText.setText("");
                matterNameView.setText("");
                matterNumView.setText("");
                break;
            case R.id.order_type_spinner:
                XPopup.get(getActivity()).asCenterList("请选择凭证类型", new String[]{"产品线上销售", "产品线下销售", "配件销售"},
                        // null, /** 图标Id数组，可无 **/
                        // 1,    /** 选中的position，默认没有选中效果 **/
                        new OnSelectListener() {
                            @Override
                            public void onSelect(int position, String text) {
                                if (adapter1 != null) {
                                    adapter1.clear();
                                    adapter1.notifyDataSetChanged();
                                }
                                orderTypeSpinner.setText(text);
                                tvTotlePostion.setText("");
                                if (position == 0) {
                                    isChanpin = 1;
                                    isOnline = 1;
                                    chanpin_ll.setVisibility(View.VISIBLE);
                                    peijian_ll.setVisibility(View.GONE);
                                    peijian_ll1.setVisibility(View.GONE);
                                    xinghao.setText("产品型号");
                                    mingcheng.setText("产品名称");
//                                    orderCode = "XSXSDD-20210624-00011";
//                                    tvOrderCode.setText("XSXSDD-20210624-00011");
//                                    onBarcodeEvent("XSDD20220310-00002");
//                                    handler.postDelayed(new Runnable() {
//                                        @Override
//                                        public void run() {
//                                            onBarcodeEvent("S1400D2SCS012134D1240");
//                                        }
//                                    },2000);
                                } else if (position == 1) {
                                    isChanpin = 1;
                                    isOnline = 2;
                                    chanpin_ll.setVisibility(View.VISIBLE);
                                    peijian_ll.setVisibility(View.GONE);
                                    peijian_ll1.setVisibility(View.GONE);
                                    xinghao.setText("产品型号");
                                    mingcheng.setText("产品名称");
                                    adapter3.notifyDataSetChanged();
//                                    onBarcodeEvent("akny05-CPCG202109060006");
//                                    orderCode = "akny05-CPCG202109060006";
//                                    tvOrderCode.setText("akny05-CPCG202109060006");
                                } else {
                                    isChanpin = 0;
                                    chanpin_ll.setVisibility(View.GONE);
                                    peijian_ll.setVisibility(View.VISIBLE);
                                    peijian_ll1.setVisibility(View.VISIBLE);
                                    xinghao.setText("配件型号");
                                    mingcheng.setText("配件名称");
                                    adapter3.notifyDataSetChanged();
//                                    onBarcodeEvent("SHWX202112230001");
////
//                                    handler.postDelayed(new Runnable() {
//                                        @Override
//                                        public void run() {
//                                            onBarcodeEvent("PJ-A-CS01-0002");
//                                        }
//                                    }, 1000);

//                                    orderCode = "AKNY05-PJCG202109270002";
//                                    tvOrderCode.setText("AKNY05-PJCG202109270002");
//                                    orderCode = "AKNY05-PJCG202109270002";
//                                    tvOrderCode.setText("AKNY05-PJCG202109270002");
                                }
                            }
                        })
                        .show();
                break;
            case R.id.peijian_type_spinner:
                XPopup.get(getActivity()).asCenterList("请选择类型", new String[]{"货架", "中转箱"},
                        // null, /** 图标Id数组，可无 **/
                        // 1,    /** 选中的position，默认没有选中效果 **/
                        new OnSelectListener() {
                            @Override
                            public void onSelect(int position, String text) {
                                peijianTypeSpinner.setText(text);
                                isBox = position == 1;
                                adapter2.notifyDataSetChanged();
                                adapter3.notifyDataSetChanged();
                                if (isBox) {
                                    tv_transit_code_menu.setVisibility(View.VISIBLE);
                                    tv_transit_code_menu_line.setVisibility(View.VISIBLE);
                                    boxCodeView.setVisibility(View.VISIBLE);
//                                    onBarcodeEvent("123456");
//                                    onBarcodeEvent("1800000139");
                                } else {
                                    tv_transit_code_menu.setVisibility(View.GONE);
                                    tv_transit_code_menu_line.setVisibility(View.GONE);
                                    boxCodeView.setVisibility(View.GONE);
//                                    onBarcodeEvent("PPS-PJ-B-1-111");
                                }
                            }
                        })
                        .show();
                break;
            case R.id.btn_chuku_save:
                if (TextUtils.isEmpty(orderTypeSpinner.getText().toString())) {
                    ToastUtil.getInstance()._short(getActivity(), "出库凭证类型不能为空");
                    return;
                }
//                if (list.size() != goodNum) {
//                    ToastUtil.getInstance()._short(getActivity(), "出库数量不正确");
//                    return;
//                }
                try {
                    if (BaseUtils.fastClick()) {
                        if (isChanpin == 1) {//产品
                            List<IntoProductSaveBean> items = new ArrayList<>();
                            JSONArray jsonArray = new JSONArray();
                            JSONObject tmpObj = null;
                            for (int i = 0; i < list.size(); i++) {
                                tmpObj = new JSONObject();
                                tmpObj.put("code", list.get(i).getGoods_code());
                                tmpObj.put("goods_model", list.get(i).getGoods_model());
                                jsonArray.put(tmpObj);
                                tmpObj = null;
                            }
                            if (jsonArray.length() > 0) {
                                String itemsString = GsonUtils.toJson(items);
                                if (isOnline == 1) {
                                    //线上
                                    mPresenter.Out03("1", tvOrderCode.getText().toString().trim(), jsonArray);
                                } else {
                                    //线下
                                    mPresenter.Out10("1", tvOrderCode.getText().toString().trim(), jsonArray);
                                }
                            } else {
                                ToastUtils.showShort("无新增产品");
                            }
                        } else {// 配件
                            List<IntoProductSaveBean> items = new ArrayList<>();
                            JSONArray jsonArray = new JSONArray();
                            JSONObject tmpObj = null;
                            for (int i = 0; i < list.size(); i++) {
                                tmpObj = new JSONObject();
                                if (isSh) {
                                    tmpObj.put("matter_code", list.get(i).getGoods_code());
                                    tmpObj.put("num", list.get(i).getCount());
                                } else {
                                    tmpObj.put("matter_code", list.get(i).getGoods_code());
                                    tmpObj.put("num", list.get(i).getCount());
                                    if (isBox) {
                                        tmpObj.put("warehouse_coordinates", list.get(i).getWarehouse_coordinates());
                                    } else {
                                        tmpObj.put("warehouse_coordinates", "");
                                    }
                                    tmpObj.put("transit_code", TextUtils.isEmpty(list.get(i).getTransit_code()) ? "" : list.get(i).getTransit_code());
                                }
                                jsonArray.put(tmpObj);
                                tmpObj = null;
                            }
                            if (jsonArray.length() > 0) {
                                String itemsString = GsonUtils.toJson(items);
                                if (!isSh) {
                                    mPresenter.Out07(tvOrderCode.getText().toString().trim(), jsonArray);
                                } else {
                                    String type = "1";
                                    if (isBox) {
                                        type = "2";
                                    }
                                    mPresenter.Out05(tvOrderCode.getText().toString().trim(), type, stockCodeView.getText().toString().trim(), boxCodeTextView.getText().toString().trim(), jsonArray);
                                }
                            } else {
                                ToastUtils.showShort("无新增产品");
                            }
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            case R.id.button:
                if (TextUtils.isEmpty(orderTypeSpinner.getText().toString())) {
                    ToastUtil.getInstance()._short(getActivity(), "出库凭证类型不能为空");
                    return;
                }
                if (isChanpin == 1) {
                    if (isOnline == 1) {
                        mPresenter.Out01("1", "2", tvOrderCode.getText().toString().trim());
                    } else {
                        mPresenter.details(tvOrderCode.getText().toString().trim());
                    }
                } else {
                    mPresenter.peiJianDetails(tvOrderCode.getText().toString().trim());
                }
                break;
            case R.id.qingkong:
                tvOrderCode.setText("");
                orderCode = "";
                list.clear();
                if (adapter1 != null) {
                    adapter1.clear();
                    adapter1.notifyDataSetChanged();
                }
                if (adapter2 != null) {
                    adapter2.clear();
                    adapter2.notifyDataSetChanged();
                }
                if (adapter3 != null) {
                    adapter3.clear();
                    adapter3.notifyDataSetChanged();
                }
                tvTotlePostion.setText("");
                playRing("qingkong.mp3");
                break;
        }
    }

    public void onBarcodeEvent(String code) {
        LogUtils.e("扫描结果2:" + code);
//        ToastUtils.showShort("扫描结果2：" + code+" stockCodeView.getText().toString().trim()="+stockCodeView.getText().toString().trim()+" stockCodeView.getVisibility()="+stockCodeView.getVisibility()+" isChanpin="+isChanpin+" tvOrderCode.getText()="+tvOrderCode.getText());
        if (isChanpin == 1) {
            if (!TextUtils.isEmpty(code)) {
                if (TextUtils.isEmpty(tvOrderCode.getText().toString().trim())) {
                    if (TextUtils.isEmpty(orderTypeSpinner.getText().toString())) {
                        ToastUtil.getInstance()._short(getActivity(), "出库凭证类型不能为空");
                        return;
                    }
                    orderCode = code;
                    if (isChanpin == 1) {
                        if (isOnline == 1) {
                            mPresenter.Out01("1", "2", code);
                        } else {
                            mPresenter.details(orderCode);
                        }
                    } else {
                        mPresenter.peiJianDetails(orderCode);
                    }
                } else {
                    mPresenter.Out04(code);
                }
            }
        } else {
            if (!TextUtils.isEmpty(code)) {
                if (TextUtils.isEmpty(tvOrderCode.getText().toString().trim())) {
                    if (TextUtils.isEmpty(orderTypeSpinner.getText().toString())) {
                        ToastUtil.getInstance()._short(getActivity(), "出库凭证类型不能为空");
                        return;
                    }
                    orderCode = code;
                    mPresenter.peiJianDetails(code);
                } else if (TextUtils.isEmpty(stockCodeView.getText().toString().trim())) {
                    //填入货架号
                    huoCode = code;
                    mPresenter.Into13(code);
                } else if (TextUtils.isEmpty(boxCodeTextView.getText().toString().trim()) && isBox) {
                    //填入中转箱号
                    xiangCode = code;
                    mPresenter.saveZx(code);
                } else if (TextUtils.isEmpty(peijianCodeEditText.getText().toString().trim())) {
                    //填入配件编号
                    peijianCodeEditText.setText(code);
                }
            }
        }
    }

    private AssetManager assetManager;
    private MediaPlayer player = new MediaPlayer();

    private void playRing(String fileName) {
        try {
            assetManager = getActivity().getAssets();
            AssetFileDescriptor fileDescriptor = assetManager.openFd(fileName);
//            if (player.isPlaying()) {
            player.reset();
//            }
            player.setDataSource(fileDescriptor.getFileDescriptor(), fileDescriptor.getStartOffset(),
                    fileDescriptor.getLength());
            player.prepare();
            player.start();
            player.setOnSeekCompleteListener(new MediaPlayer.OnSeekCompleteListener() {
                @Override
                public void onSeekComplete(MediaPlayer mp) {
                    player.stop();
                }
            });

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void playUrl(String fileName, final int i) {
        if (TextUtils.isEmpty(fileName)) {
            if (i == 1) {
                playRing("luruchenggong.mp3");
                return;
            } else {
                playRing("wuxiao.mp3");
                return;
            }
        }
        try {
            player.reset();
            if (!fileName.contains("http")) {
                player.setDataSource(BaseUrl.mp3Url + fileName);
            } else {
                player.setDataSource(fileName);
            }
            player.prepare();
            player.start();
            player.setOnSeekCompleteListener(new MediaPlayer.OnSeekCompleteListener() {
                @Override
                public void onSeekComplete(MediaPlayer mp) {
                    if (i == 1) {
                        playRing("luruchenggong.mp3");
                    } else {
                        playRing("wuxiao.mp3");
                    }
                }
            });
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onPause() {
        if (player.isPlaying()) {
            player.stop();
        }
        super.onPause();
    }

    @Override
    public void onDestroy() {
        player.release();
        super.onDestroy();
    }
}
